package com.ruoyi.weiriya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.weiriya.service.IWCompanyService;
import com.ruoyi.weiriya.service.IWUserAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.weiriya.mapper.WVehicleMapper;
import com.ruoyi.common.core.domain.entity.WVehicle;
import com.ruoyi.weiriya.service.IWVehicleService;

import java.util.ArrayList;
import java.util.List;

/**
 * 车辆信息Service业务层处理
 * 
 * @author lynn
 * @date 2023-08-01
 */
@Service
public class WVehicleServiceImpl extends ServiceImpl<WVehicleMapper, WVehicle> implements IWVehicleService
{
    @Autowired
    private WVehicleMapper vehicleMapper;


    @Autowired
    private IWCompanyService companyService;

    @Override
    public List<WVehicle> getList(WVehicle wVehicle) {
        QueryWrapper<WVehicle> queryWrapper = wVehicle.queryWrapper();
        if(SecurityUtils.getLoginUser().isAdministrators() && StringUtils.isNotBlank(SecurityUtils.getLoginUser().getUser().getCompanyId())) {
            List<String> areaIdListByUserId = companyService.getAreaIdList();
            if(areaIdListByUserId.size() > 0) {
                queryWrapper.in("a.id", areaIdListByUserId);
            } else {
                return new ArrayList<>();
            }
        }
        return vehicleMapper.getList(queryWrapper);
    }

    @Override
    public WVehicle getWVehicleById(String id) {
        return vehicleMapper.getWVehicleById(id);
    }

    @Override
    public WVehicle getWVehicleByMemberId(String id) {
        return vehicleMapper.getWVehicleByMemberId(id);
    }

    @Override
    public boolean saveWVehicle(WVehicle wVehicle) {
        checkVehicle(wVehicle);
        WVehicle vehicle = this.getWVehicleByMemberId(wVehicle.getMemberId());
        if(StringUtils.isNotNull(vehicle)){
            throw new ServiceException("该用户已存在车辆！");
        }
        return this.save(wVehicle);
    }

    @Override
    public boolean updateWVehicleById(WVehicle wVehicle) {
        checkVehicle(wVehicle);
        WVehicle vehicle = this.getById(wVehicle.getId());
        vehicle.setVehicleChargeTypeId(wVehicle.getVehicleChargeTypeId());
        vehicle.setVehicleNonChargeTypeId(wVehicle.getVehicleNonChargeTypeId());
        vehicle.setVehicleBrand(wVehicle.getVehicleBrand());
        vehicle.setVehicleNumber(wVehicle.getVehicleNumber());
        // 更换图片
        if(StringUtils.isNotBlank(wVehicle.getVehicleImgUrl()) && !wVehicle.getVehicleImgUrl().equals(vehicle.getVehicleImgUrl())) {
            if(StringUtils.isNotBlank(wVehicle.getVehicleImgUrl())) {
                String path = vehicle.getVehicleImgUrl();
                path = RuoYiConfig.getProfile() + path.replaceAll(Constants.RESOURCE_PREFIX, "");
                FileUploadUtils.remove(path);
            }
            vehicle.setVehicleImgUrl(wVehicle.getVehicleImgUrl());
        }

        return this.updateById(vehicle);
    }


    private void checkVehicle(WVehicle wVehicle) {
        if(StringUtils.isBlank(wVehicle.getVehicleChargeTypeId())){
            throw new ServiceException("请选择车辆充电类型！");
        }
        if(StringUtils.isBlank(wVehicle.getVehicleNonChargeTypeId())){
            throw new ServiceException("请选择车辆非充电类型！");
        }
    }
}
